// pages/jobHunting/jobHunting.js
var that,
  app = getApp(),
  api = new (require('../../utils/api.js')),
  util = new (require('../../utils/util.js')),
  headimg = api.getimgurl();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
    activeIdx: 1,
    employmentList: [
      {
        title: '广东省东莞市-招瓦工班',
        num: 6,
        price: '面议',
        detail: '工地生活区厨房厕所冲凉房水沟，砌筑抹灰，贴墙地砖，包工面议'
      },
      {
        title: '广东省东莞市-招瓦工班',
        num: 6,
        price: '面议',
        detail: '工地生活区厨房厕所冲凉房水沟，砌筑抹灰，贴墙地砖，包工面议'
      },
      {
        title: '广东省东莞市-招瓦工班',
        num: 6,
        price: '面议',
        detail: '工地生活区厨房厕所冲凉房水沟，砌筑抹灰，贴墙地砖，包工面议'
      },
      {
        title: '广东省东莞市-招瓦工班',
        num: 6,
        price: '面议',
        detail: '工地生活区厨房厕所冲凉房水沟，砌筑抹灰，贴墙地砖，包工面议'
      },
    ],
    range: [[], [{ name: '全部', id: 0 }]],
    worktypelist: [[], [{ name: '全部', id: 0 }]],
    primarytypeList: [],//一级工种分类列表
    subtypeList: [],//二级工种分类列表
    worktype: 0,//一级工种id
    worksubtype: 0,//二级工种id
    worktypename: '',//一级工种
    worksubtypename: '',//二级工种
    provincelist: [],
    citylist: [],
    province: '',
    city: '',
    pagesize: 20,
    start: 1,
    recruitlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    if (app.logininfo) {
      this.getdata();
    } else {
      util.setwatcher(app, this.afterlogin)
    }
    wx.hideTabBar()
  },
  afterlogin: {
    logininfo: res => {
      that.getdata();
    }
  },
  getdata() {
    wx.showLoading({
      title: '加载中...',
    })
    let { primarytypeList, subtypeList, worktypelist, provincelist, citylist, range, recruitlist } = that.data, newslist = [{ name: '全部', id: 0 }]
    api.getprovincelist(res => {
      wx.hideLoading()
      if (res.data.issuccess == 1) {
        provincelist = newslist.concat(res.data.list)
        range[0] = provincelist
        that.setData({ provincelist, range })
      }
    })
    api.getallcitylist(res => {
      if (res.data.issuccess == 1) {
        citylist = res.data.list
        that.setData({ citylist })
      }
    })
    api.getsubMechanicstypelist(res => {
      if (res.data.issuccess == 1) {
        primarytypeList = newslist.concat(res.data.list)
        subtypeList = res.data.sublist
        worktypelist[0] = primarytypeList
        that.setData({ worktypelist, subtypeList, primarytypeList })
      }
    })
    that.getlist()
  },
  getlist() {
    let { pagesize, start, province, city, worktype, worksubtype ,recruitlist} = that.data
    province = province == '全部' ? '' : province
    city = city == '全部' ? '' : city
    api.getallRecruitlist({ pagesize, start, province, city, worktype, worksubtype }, res => {
      if (res.data.issuccess == 1) {
        let { total, list } = res.data
        if (list.length > 0) {
          list.forEach(x => {
            x['date']=util.formatTime(new Date(x.updateDate*1000))
            recruitlist.push(x)
          })
          that.setData({recruitlist,total})
        }
        console.log(list, 'list');
      }
    })
  },
  //地区
  changeRange(e) {
    console.log(e, 'eeeeeee');
    let { range, province, city } = that.data, { value } = e.detail
    province = range[0][value[0]].name
    city = range[1][value[1]].name
    that.setData({ province, city })
  },
  RangeColumnChange(e) {
    let { column, value } = e.detail, { provincelist, citylist, range } = that.data, list = [], newslist = [{ name: '全部', id: 0 }]
    if (column == 0) {
      citylist.forEach(x => {
        if (provincelist[value].id == x.cid) {
          list.push(x)
        }
      })
      list = newslist.concat(list)
      range[1] = list
    }
    that.setData({ range })
  },
  //工种
  changeType(e) {
    console.log(e, 'eeeeeee');
    let { worktypelist, worktype, worksubtype, worktypename, worksubtypename } = that.data, { value } = e.detail
    worktype = worktypelist[0][value[0]].id
    worksubtype = worktypelist[1][value[1]].id
    worktypename = worktypelist[0][value[0]].name
    worksubtypename = worktypelist[1][value[1]].name
    that.setData({ worktypename, worktype, worksubtype, worksubtypename })
  },
  TypeColumnChange(e) {
    let { column, value } = e.detail, { worktypelist, subtypeList, primarytypeList } = that.data, list = [], newslist = [{ name: '全部', id: 0 }]
    if (column == 0) {
      subtypeList.forEach(x => {
        if (primarytypeList[value].id == x.cid) {
          list.push(x)
        }
      })
      list = newslist.concat(list)
      worktypelist[1] = list
    }
    that.setData({ worktypelist })
  },
  todetail(e){
    let {id} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../recruitmentDetail/recruitmentDetail?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
  },

  /**
   * 监听页面滑动事件
   */
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
    // console.log(scrollTop,'scrollTopscrollTop');
    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop == 0 ? false : true;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }
    that.setData({
      isFixedTop: isSatisfy
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})